<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class jr_web_fetcher_script extends JR_Module
{
	private function build_proxy_script($cls_name,$proxy)
	{ 
		$cls = $this->$cls_name;
		// $scripts[] =  "<script>" ; 
		$scripts[] = "var $cls_name = new Object();" ;  
		$cls_methods = get_class_methods($cls);
		foreach($cls_methods as $cls_method)
		{  
			$cls_method_ori = $cls_method;
			$cls_method = preg_replace("/^delete$/i", "del", $cls_method);
			$scripts[] = "$cls_name.$cls_method = function(){";
			$scripts[] = "  var len_arg = arguments.length;";
			// $scripts[] = "  alert(len_arg);";
			$scripts[] = "  var call_back_func = arguments[len_arg-1];";
			$scripts[] = "	var param = {'cls_name':'$cls_name','method_name':'$cls_method_ori'};";  
			$scripts[] = "	for(i=0;i<len_arg-1;i++){var arg=arguments[i];";
			$scripts[] = "		if(typeof(arg)=='object'){";
			// $scripts[] = "			alert(JSON.stringify(arg));";
			$scripts[] = "			eval(\"param.arg\"+i+\"='\"+JSON.stringify(arg)+\"';\");";       
			$scripts[] = "		}else{"; 
			$scripts[] = "			eval(\"param.arg\"+i+\"= arg ;\");"  ;      
			$scripts[] = "		}";  
			$scripts[] = "	}"; 
			// $scripts[] = "	alert(param.arg1);"; 
			// $scripts[] = "  param_str = param_str.replace(/(^\\/\\s*)|(\\s*\\/$)/g, '');";
			$scripts[] = "  var url = '"._U("module/jr_web_fetcher/script/$proxy")."';";  
			$scripts[] = "  $.post(url,param,function(data){";
			$scripts[] = "		eval('var data='+data);call_back_func(data);";
			$scripts[] = "	});" ;  
			// $scripts[] = "  alert(url);";
			$scripts[] = "}" ; 
		} 
		// $scripts[] = "</script>" ;
		header('Content-Type:application/javascript;charset=utf-8');
		echo implode("\r\n",$scripts) ;  
	}
	
	private function unset_property(&$ob,$ignore_list)
	{
		foreach($ignore_list as $ignore)
		{
			if(isset($ob->$ignore))
			{
				unset($ob->$ignore); 
			}
		}
	}
	
	private function build_json_result($class_name,$method_name,$params,$ignore_list = Array())
	{
		$p = Array();
		foreach($params as $value)
		{
			if(preg_match("/^{\".*?\":\".*?\"}$/i", $value))
			{
				$p[] = json_decode($value,true) ;
			}
			else
			{
				$p[] = $value;
			}
		}
		// print_r($p);exit;
		if($class_name == null)
		{
			$result =
				call_user_func_array($method_name, $p);
		}
		else
		{
			$result =
				call_user_func_array(array($this->$class_name,$method_name), $p);
		}
		// echo $model_name;
		
		if(is_array($result))
		{
			$rows = Array();
			foreach($result as $row)
			{
				if(is_object($row))
				{
					$this->unset_property($row,$ignore_list ) ;  
				}
				// $rows[] = $this->get_json_string($ignore_list, $row) ;
			}
			echo json_encode($result); 
			// echo "[".implode(",",$rows)."]"  ;
		}
		else if(is_object($result))
		{
			$this->unset_property($result,$ignore_list ) ; 
			echo  json_encode($result);
		}
		else
		{
			echo json_encode(array('result'=>$result)) ; 
		}  
	}
	
	public function models($model_name)
	{
		if(!isset($this->$model_name))
		{
			$this->load->model($model_name) ;
		}
		$this->build_proxy_script($model_name, "model_proxy");
	}
	
	public function model_proxy()
	{
		// print_r($_POST);
		// print_r(json_decode($_POST['arg0'],true));exit;
		$model_name = array_remove($_POST, "cls_name");
		$method_name = array_remove($_POST, "method_name");
		$self  = str_replace("_manager", "", $model_name);
		$ignore_list = array($self);
		
		$this->build_json_result($model_name, $method_name, $_POST,$ignore_list);  
	}
	
	public function libraries($lib_name)
	{
		if(!isset($this->$lib_name))
		{
			$this->load->library($lib_name) ;
		}
		$this->build_proxy_script($lib_name, "lib_proxy");  
	}
	
	public function lib_proxy()
	{
		$lib_name = array_remove($_POST, "cls_name");
		$method_name = array_remove($_POST, "method_name");
	
		$this->build_json_result($lib_name, $method_name, $_POST);
	}
	  
	public function helpers($helper_name)
	{
		$scripts[] = "var $helper_name = new Object();";
		$scripts[] = "$helper_name.call = function(){";
		$scripts[] = "  var len_arg = arguments.length;";
		// $scripts[] = "  alert(len_arg);";
		$scripts[] = "  var method_name = arguments[0];";
		$scripts[] = "  var call_back_func = arguments[len_arg-1];";
		$scripts[] = "	var param = {'helper_name':'$helper_name','method_name':method_name};";
		$scripts[] = "	for(i=1;i<len_arg-1;i++){var arg=arguments[i];";
		$scripts[] = "		if(typeof(arg)=='object'){";
		// $scripts[] = "			alert(JSON.stringify(arg));";
		$scripts[] = "			eval(\"param.arg\"+i+\"='\"+JSON.stringify(arg)+\"';\");";
		$scripts[] = "		}else{";
		$scripts[] = "			eval(\"param.arg\"+i+\"= arg ;\");";
		$scripts[] = "		}";
		$scripts[] = "	}";
		// $scripts[] = "	alert(param.arg1);";
		// $scripts[] = "  param_str = param_str.replace(/(^\\/\\s*)|(\\s*\\/$)/g, '');";
		$scripts[] = "  var url = '"._U("module/jr_web_fetcher/script/help_proxy")."';";
		$scripts[] = "  $.post(url,param,function(data){";
		$scripts[] = "		eval('var data='+data);call_back_func(data);";
		$scripts[] = "	});" ;
		// $scripts[] = "  alert(url);";
		$scripts[] = "}" ;
		header('Content-Type:application/javascript;charset=utf-8');
		echo implode("\r\n",$scripts) ; 
	}
	
	public function help_proxy()
	{
		$helper_name = array_remove($_POST, "helper_name");
		$this->load->helper($helper_name);
		$method_name = array_remove($_POST, "method_name");
		$this->build_json_result(null, $method_name, $_POST); 
	}
}
